----------------------------------------------
-- Awesome theme for awesome4.x
--   by D1S12T                      --
----------------------------------------------





local theme_assets = require("beautiful.theme_assets")
local dpi = require("beautiful.xresources").apply_dpi
local set_dpi = require("beautiful.xresources").set_dpi
local gfs = require("gears.filesystem")
local themes_path = gfs.get_themes_dir()
local gears_shape = require("gears.shape")
local wibox = require("wibox")
local awful_widget_clienticon = require("awful.widget.clienticon")
local gtk = require("beautiful.gtk")
-- set_dpi(144)

-- Helper functions for modifying hex colors:
--
local hex_color_match = "[a-fA-F0-9][a-fA-F0-9]"
local function darker(color_value, darker_n)
    local result = "#"
    local channel_counter = 1
    for s in color_value:gmatch(hex_color_match) do
        local bg_numeric_value = tonumber("0x"..s)
        if channel_counter <= 3 then
            bg_numeric_value = bg_numeric_value - darker_n
        end
        if bg_numeric_value < 0 then bg_numeric_value = 0 end
        if bg_numeric_value > 255 then bg_numeric_value = 255 end
        result = result .. string.format("%02x", bg_numeric_value)
        channel_counter = channel_counter + 1
    end
    return result
end
local function is_dark(color_value)
    local bg_numeric_value = 0;
    local channel_counter = 1
    for s in color_value:gmatch(hex_color_match) do
        bg_numeric_value = bg_numeric_value + tonumber("0x"..s);
        if channel_counter == 3 then
            break
        end
        channel_counter = channel_counter + 1
    end
    local is_dark_bg = (bg_numeric_value < 383)
    return is_dark_bg
end
local function mix(color1, color2, ratio)
    ratio = ratio or 0.5
    local result = "#"
    local channels1 = color1:gmatch(hex_color_match)
    local channels2 = color2:gmatch(hex_color_match)
    for _ = 1,3 do
        local bg_numeric_value = math.ceil(
          tonumber("0x"..channels1())*ratio +
          tonumber("0x"..channels2())*(1-ratio)
        )
        if bg_numeric_value < 0 then bg_numeric_value = 0 end
        if bg_numeric_value > 255 then bg_numeric_value = 255 end
        result = result .. string.format("%02x", bg_numeric_value)
    end
    return result
end
local function reduce_contrast(color, ratio)
    ratio = ratio or 50
    return darker(color, is_dark(color) and -ratio or ratio)
end

local function choose_contrast_color(reference, candidate1, candidate2)  -- luacheck: no unused
    if is_dark(reference) then
        if not is_dark(candidate1) then
            return candidate1
        else
            return candidate2
        end
    else
        if is_dark(candidate1) then
            return candidate1
        else
            return candidate2
        end
    end
end


-- inherit xresources theme:
-- local theme = dofile(themes_path.."xresources/theme.lua")
local theme = dofile(themes_path.."xresources/theme.lua")
-- load and prepare for use gtk theme:
theme.gtk = gtk.get_theme_variables()
-- Test


-- My Variables

-- tray

whitesur_dark="#242424"
whitesur_white="#ffffff"
whitesur_grey="#ffffff"
whitesur_blue_active="#315bef"
whitesur_blue_inactive="#87a3ef"
whitesur_hover_border="#538fff"
whitesur_focus_="#538fff"

-- Color Defines
my_font_color = whitesur_dark
my_bg_unfocus_color = "#787878"

my_bar_color = "#242424"
my_bg_color = "#242424"
my_focus_color = "#315bef"
my_normal_color = "#242424"
my_minimize_color = "#C3C7D1"
my_urgent_color = "#ff0000"
my_fg_color      = "#C3C7D1"
my_fg_color      = "#00ff00"

my_focus_text_color="#C3C7D1"
my_unfocus_text_color="#C3C7D1"
-- my_focus_bg_color = "#161925"
-- my_unfocus_bg_color = "#161925"
my_select_bg_color = "#315bef"
my_focus_color = "#315bef"
my_normal_color = "#242424"
my_focus_fg_color = "#fefefe"
my_unfocus_fg_color = "#6d707b"
my_occupied_fg_color = "#315bef"
my_base_color = my_test_color
-- my_base_color = "#181b28"
my_urgente_color = "#e6133e"
my_fg_color      = "#C3C7D1"
my_test_color      = "#00ff00"

---


-------------------------------------------------------------------------------------
theme.font_size = "9"
theme.font_family ="WenQuanYi Zen Hei " 
theme.font          = theme.font_family .. theme.font_size
if not theme.gtk then
    local gears_debug = require("gears.debug")
    gears_debug.print_warning("Can't load GTK+3 theme. Using 'xresources' theme as a fallback.")
    return theme
end
-- theme.gtk.button_border_radius = dpi(0)
-- theme.gtk.button_border_width = dpi(2)
-- theme.gtk.button_border_width = dpi(0)
theme.gtk.bold_font = theme.font_family .. ' Bold ' .. theme.font_size
theme.gtk.menubar_border_color = my_font_color
theme.gtk.menubar_border_width = dpi(0)
theme.wibar_bg           = my_bar_color
theme.wibar_fg           = my_font_color



theme.fg_focus           = "#dedede"
theme.fg_urgent          = "#da4453"

theme.bg_normal          = "#242424"
theme.bg_urgent          = "#242424"
theme.bg_minimize        = my_minimize_color
theme.fg_minimize        = my_font_color
theme.bg_systray         = my_bar_color

-- 窗口边框
theme.border_normal      = "#242424"
theme.border_focus       = "#315bef"
theme.border_width       = dpi(3)
theme.border_radius      = dpi(0)
theme.useless_gap        = dpi(3)
theme.tasklist_fg_focus  = "#315bef"
theme.tasklist_fg_normal = "#dedede"
theme.tasklist_bg_focus  = "#242424"
theme.tasklist_bg_normal = "#242424"
theme.taglist_bg_focus = "#242424"
theme.taglist_bg_occupied = "#242424"
theme.taglist_bg_empty = "#242424"
theme.taglist_fg_empty = "#dedede"
theme.taglist_fg_focus = "#315bef"
theme.taglist_fg_occupied = "#4f7fef"


theme.titlebar_bg_normal  = "#242424"
theme.titlebar_bg_focus   = "#242424"
theme.titlebar_fg_normal  = "#dedede"
theme.titlebar_fg_focus   = "#dedede"

-- 工具提设置
theme.tooltip_fg          = "#dedede"
theme.tooltip_bg          = "#656565"


-- 菜单栏颜色
theme.menu_border_width   = dpi(2)
theme.menu_border_color   = my_fg_color
theme.menu_bg      = my_bg_color
theme.menu_bg_focus      = my_focus_color
theme.menu_fg_normal      = my_font_color
theme.menu_fg_focus       = my_font_color

theme.menu_height = dpi(24)
theme.menu_width  = dpi(150)
theme.menu_submenu_icon = nil
theme.menu_submenu = "▸ "



local rounded_rect_shape = function(cr,w,h)
    gears_shape.rounded_rect(
        cr, w, h, theme.border_radius
    )
end


theme.tasklist_shape_minimized = rounded_rect_shape
theme.tasklist_shape_border_color_minimized = mix(
    theme.bg_minimize,
    theme.fg_minimize,
    0.85
)
theme.tasklist_shape_border_width_minimized = theme.gtk.button_border_width

theme.tasklist_spacing = theme.gtk.button_border_width


theme.tasklist_widget_template = {
    {
        {
            {
                {
                    id     = 'clienticon',
                    widget = awful_widget_clienticon,
                },
                margins = dpi(4),
                widget  = wibox.container.margin,
            },
            {
                id     = 'text_role',
                widget = wibox.widget.textbox,
            },
            layout = wibox.layout.fixed.horizontal,
        },
        left  = dpi(2),
        right = dpi(4),
        widget = wibox.container.margin
    },
    id     = 'background_role',
    widget = wibox.container.background,
    create_callback = function(self, c)
        self:get_children_by_id('clienticon')[1].client = c
    end,
}

-- }}}




-- Recolor Layout icons:
theme = theme_assets.recolor_layout(theme, theme.wibar_fg)

-- Recolor titlebar icons:
--
theme = theme_assets.recolor_titlebar(
    theme, theme.titlebar_fg_normal, "normal"
)
theme = theme_assets.recolor_titlebar(
    theme, reduce_contrast(theme.titlebar_fg_normal, 20), "normal", "hover"
)
theme = theme_assets.recolor_titlebar(
    theme, theme.gtk.error_bg_color, "normal", "press"
)
theme = theme_assets.recolor_titlebar(
    theme, theme.titlebar_fg_focus, "focus"
)
theme = theme_assets.recolor_titlebar(
    theme, reduce_contrast(theme.titlebar_fg_focus, 20), "focus", "hover"
)
theme = theme_assets.recolor_titlebar(
    theme, theme.gtk.error_bg_color, "focus", "press"
)

-- Define the icon theme for application icons. If not set then the icons
-- from /usr/share/icons and /usr/share/icons/hicolor will be used.
theme.icon_theme = "/home/feng/.config/awesome/themes/icons/Papirus/"

-- Generate Awesome icon:
theme.awesome_icon = theme_assets.awesome_icon(
    theme.menu_height, mix(theme.bg_focus, theme.fg_normal), theme.wibar_bg
)

-- Generate taglist squares:
--local taglist_square_size = dpi(4)
--theme.taglist_squares_sel = theme_assets.taglist_squares_sel(
    --taglist_square_size, theme.gtk.header_button_border_color
--)
--theme.taglist_squares_unsel = theme_assets.taglist_squares_unsel(
    --taglist_square_size, theme.gtk.header_button_border_color
--)
-- Or disable them:
theme.taglist_squares_sel = nil
theme.taglist_squares_unsel = nil

-- Generate wallpaper:
local wallpaper_bg = theme.gtk.base_color
local wallpaper_fg = theme.gtk.bg_color
local wallpaper_alt_fg = theme.gtk.selected_bg_color
if not is_dark(theme.bg_normal) then
    wallpaper_bg, wallpaper_fg = wallpaper_fg, wallpaper_bg
end
wallpaper_bg = reduce_contrast(wallpaper_bg, 50)
wallpaper_fg = reduce_contrast(wallpaper_fg, 30)
wallpaper_fg = mix(wallpaper_fg, wallpaper_bg, 0.4)
wallpaper_alt_fg = mix(wallpaper_alt_fg, wallpaper_fg, 0.4)
theme.wallpaper = function(s)
    return theme_assets.wallpaper(wallpaper_bg, wallpaper_fg, wallpaper_alt_fg, s)
end


-- file = io.open("/tmp/awesome_debug.txt","w")
-- io.output(file)
-- gtk.font_size=65
-- io.write(gtk.font_size)
-- io.close(file)
return theme

